import axios from 'axios';
import Vue from 'vue'; 
import {baseurl} from './main.js';
//console.log(baseurl);

// 凑0
function padLeft (str, len, char) {
	var isStr = str.toString();
	let strlen = isStr.length;
	let newStr = "";
	if (strlen < len) {
	  for (let i = 0; i < len - strlen; i++) {
		newStr = newStr + char;
	  }
	  newStr = newStr + isStr;
	} else if (strlen > len) {
	  newStr = isStr.substr(isStr.length - len);
	} else {
	  newStr = isStr;
	}
	return newStr;
  };
  
// 所有用户的下拉
function getUsers() {
	var users = [];
	return axios({
		method: 'post',
		url: 'api/user/all',
		data: { }
	}).then(res => {
		//console.log(res.data)
		// console.log('1用户',res);
		if (res && res.data.result == "success") {
			for (let i = 0; i < res.data.data.length; i++) {
				let option = {
					value: res.data.data[i].id,
					label: res.data.data[i].user_name
				}
				users.push(option);
			}
			return users;
		} else {
			return [];
		}
	}).catch(error => {
		console.log(error);
		return [];
	});
}; 

// 所有项目的下拉框数据
function getAllProject() {
	var options = [];
	return axios({
		method: 'post',
		url: 'api/product/all',
		data: {}
	}).then(res => {
		if (res && res.data.result == "success") {
			// console.log('5678567871',res.data);
			for (let i = 0; i < res.data.data.length; i++) {
				let option = {
					value: res.data.data[i].id,
					label: res.data.data[i].product_name,
					equips_count: res.data.data[i].equips_count,
				    views_count: res.data.data[i].views_count,
					cameras_count:res.data.data[i].cameras_count
				}
				options.push(option);
			}
			return options;

		} else {
			return [];
		}
	}).catch(error => {
		console.log(error);
		return [];
	});
};   

// 所有设备名称的下拉框
function getEquipAll(id,type=1){
	var equip= []; 
	return axios({
		method: 'post',
		url: 'api/equip/all',
		data: {
			"product_id":id,
			"type":type
		}
	}).then(res => {
		if (res && res.data.result == "success") {
			// console.log('2',res);
			for (let i = 0; i < res.data.data.length; i++) {
				let option = {
					value: res.data.data[i].id,
					label: res.data.data[i].equip_name,
					type: 1,
					disabled:Number(res.data.data[i].sensor_count)==0,
					slaveCount: res.data.data[i].slave_count
				}
				equip.push(option);
			}
			return equip
		} else {
			return [];
		}
	}).catch(error => {
		console.log(error);
		return [];
	});

}
//所有器具
function getInstrumentAll(id=0){
	var instruments= []; 
	return axios({
		method: 'post',
		url: 'api/instrument/all',
		data: {
			"project_id":id
		}
	}).then(res => {
		if (res && res.data.result == "success") {
			// console.log('2',res);
			for (let i = 0; i < res.data.data.length; i++) {
				let option = {
					value: res.data.data[i].id,
					label: res.data.data[i].instrument_name,
				}
				instruments.push(option);
			}
			return instruments
		} else {
			return [];
		}
	}).catch(error => {
		console.log(error);
		return [];
	});

}


// 器具类型的下拉框
function getAllUtensilType() {
	var types = [];
	return axios({
		method: 'post',
		url: 'api/instrumentType/all',
		data: { }
	}).then(res => {
		//console.log(res.data)
		if (res && res.data.result == "success") {
			// console.log('器具类型',res);
			for (let i = 0; i < res.data.data.length; i++) {
				let option = {
					value: res.data.data[i].id,
					label: res.data.data[i].instrument_type_name,
					count: res.data.data[i].instruments_count
					//count:2
				}
				types.push(option);
			}
			return types;
		} else {
			return [];
		}
	}).catch(error => {
		console.log(error);
		return [];
	});
};
//文章类型下拉
function getArticleTypes() {
	var types = [];
	return axios({
		method: 'post',
		url: 'api/articleType/all',
		data: { }
	}).then(res => {
		//console.log(res.data)
		if (res && res.data.result == "success") {
			//console.log(res.data.data);
			for (let i = 0; i < res.data.data.length; i++) {
				let option = {
					value: res.data.data[i].id,
					label: res.data.data[i].article_type_name,
					count: res.data.data[i].articles_count
					//count:2
				}
				types.push(option);
			}
			return types;
		} else {
			return [];
		}
	}).catch(error => {
		console.log(error);
		return [];
	});
};

export {
	getUsers,
	padLeft,
	getEquipAll,
	getInstrumentAll,
    getAllProject,
	getArticleTypes,
	getAllUtensilType
}